<template>
  <div class="login-container">
    <div class="logo">
      <h1>OST 云端网盘</h1>
    </div>
    <div class="login-form-container">
      <div class="login-form">
        <h2 class="login-title">账号密码登录</h2>

        <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginRules"
            @submit.prevent="handleLogin"
            >
          <el-form-item prop="username">
            <el-input
                v-model="loginForm.username"
                placeholder="账号/邮箱/用户名"
                prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>

          <el-form-item prop="password">
            <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="密码"
                prefix-icon="el-icon-lock"
                show-password
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
          </el-form-item>

          <el-form-item>
            <el-button
                type="primary"
                native-type="submit"
                class="login-btn"
                :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>

        <div class="login-links">
          <router-link to="/register" class="link">立即注册</router-link>
          <span class="divider">|</span>
          <router-link to="/forget" class="link">忘记密码?</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {ref,reactive} from "vue";
import { useRouter } from 'vue-router'
import {ElMessage} from "element-plus";

import{login} from '@/api/login'

const router = useRouter()
const loginFormRef = ref(null)

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: '',
  rememberMe: false
})

// 登录规则
const loginRules = reactive({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
})

// 加载状态
const loading = ref(false)

// 登录方法
const handleLogin = async () => {
  // 验证表单
  const valid = await loginFormRef.value.validate()
  if (!valid) return
  loading.value = true
  try {
    const res = await login(loginForm.username, loginForm.password)
    if (res.code === 200) {
      ElMessage.success("登录成功")
      // 存储用户
      localStorage.setItem('userInfo', JSON.stringify(res.data));
      // 存储 token
      //localStorage.setItem('token', res.data.token);
      await router.push('/')
    } else {
      ElMessage.error(res.msg)
    }
  } catch (error) {
    console.error('登录失败:', error)
    if (error.response) {
      console.error('响应数据:', error.response.data);
    }
    ElMessage.error('登录失败，请检查用户名和密码');
  } finally {
    loading.value = false
  }
}

</script>

<style scoped>
.login-container {
  display: flex;
  height: 100vh;
  background-image: url('../assets/imgs/loginBackgroundImg.png');
  background-size: cover;
  background-position: center;
  position: relative;
}

.logo {
  position: absolute;
  top: 20px;
  left: 40px;
  color: white;
  font-size: 24px;
  font-weight: bold;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
}
.top-right ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: row;
}
.top-right li {
  margin-right: 20px;
  color: white;
}
.top-right li .link:hover {
  color: #409EFF;
}

.login-form-container {
  flex: 1;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding-right: 10%;
}

.login-form {
  width: 400px;
  padding: 40px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.login-btn {
  width: 100%;
  margin-top: 10px;
}

.login-links {
  margin-top: 20px;
  text-align: center;
  font-size: 14px;
  color: lightskyblue;
}

.login-links .link {
  color: #666;
  text-decoration: none;
  transition: color 0.3s;
}

.login-links .link:hover {
  color: #409EFF;
}

.login-links .divider {
  margin: 0 10px;
  color: #ddd;
}
</style>